package com.ymatou.iapi.csim.testcase;

import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.csim.parameter.ActiveAccessBean;
import com.ymatou.iapi.csim.parameter.SendMessageBean;
import com.ymatou.iapi.csim.parameter.SendMessageCSBean;
import com.ymatou.iapi.csim.service.ActiveAccessCall;
import com.ymatou.iapi.csim.service.SendMessageCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CsimCallService;
import com.ymttest.database.model.ymt_cssession;
import com.ymttest.database.model.ymt_queuesession;
import com.ymttest.database.sqlwapper.CSIM.ymt_cssessionWapper;
import com.ymttest.database.sqlwapper.CSIM.ymt_onlinecsWapper;
import com.ymttest.database.sqlwapper.CSIM.ymt_queuesessionWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.TestDataManager;
/**
*主动从队列发起会话
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ActiveAccess{
	private static ActiveAccessBean activeaccessBean;
	private static ActiveAccessCall activeaccessCall;
	private CsimCallService service=new CsimCallService();
	private Integer csId=Integer.parseInt(EnvSetup.getData("csId").toString());
	private static Integer userId=Integer.parseInt(EnvSetup.getData("userId_IM").toString());
	private static ymt_cssessionWapper csSessionWapper=new ymt_cssessionWapper();
	private static ymt_onlinecsWapper onlinecsWapper=new ymt_onlinecsWapper();
    private ymt_queuesessionWapper queuesessionWapper=new ymt_queuesessionWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("客服主动找会话队列发起会话");
	}

	@Before
	public void caseUp() {
		activeaccessBean=new ActiveAccessBean();
		activeaccessCall=new ActiveAccessCall();
		onlinecsWapper.deleteBy(MapUtil.hashMap("CSId",csId));
		csSessionWapper.deleteByUserId(MapUtil.hashMap("CSId",csId,"UserId",userId,"Status",0));
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(activeaccessCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ActiveAccess_001() {
		Logger.start(true,"客服主动找会话队列发起会话");
		try {
            //发去会话进入队列
			Logger.comment("用户发消息进入队列");
			SendMessageBean bean=service.setSendMessageBean();
		    bean.setUserId(userId);
            bean.setMessage("自动化测试_"+TestDataManager.getRandomIntNum(5));
            bean.setMessageSource(1);
            bean.setSourceParams("App");
            bean.setCallType(0);//0普通用户 1专属用户
            bean.setQuestionGuideId(1);
            bean.setAppId("imyy.ymatou.com");
	        SendMessageCall call= service.setSendMessageCall(bean);
	        
	        
	        ymt_queuesession queusesssion=queuesessionWapper.selectBy(MapUtil.hashMap("userId",userId)).get(0);
			//客服上线
			service.getOnlineCS(csId);
			activeaccessBean.setCsId(csId);
			activeaccessBean.setQueueSessionId(queusesssion.getId());
			activeaccessCall.setData(activeaccessBean);
			activeaccessCall.callService();			
			
			Logger.verifyEquals("0", activeaccessCall.getResult().get("ErrorCode").toString(), "验证errorCode");
			Logger.verifyEquals(true, activeaccessCall.getResult().get("Success"), "验证Success");
			Logger.verifyEquals("200", activeaccessCall.getStatus(), "验证Status");
			//ymt_csSession新增数据
			ymt_cssession csssession=csSessionWapper.selectBy(MapUtil.hashMap("CSId",csId,"UserId",userId,"Status",0));
			Logger.verifyEquals(csssession.getId(), activeaccessCall.getResult().get("SessionId"), "验证SessionId");
			//ymt_queuesession没有数据
			int count=queuesessionWapper.selectBy_Count(MapUtil.hashMap("userId",userId));
			Logger.verifyEquals(0, count, "验证ymt_queuesession没有此条数据");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ActiveAccess_002() {
		Logger.start(true,"客服主动找会话队列发起会话-客服不存在");
		try {
            //发去会话进入队列
			Logger.comment("用户发消息进入队列");
			SendMessageBean bean=service.setSendMessageBean();
		    bean.setUserId(userId);
            bean.setMessage("自动化测试_"+TestDataManager.getRandomIntNum(5));
            bean.setMessageSource(1);
            bean.setSourceParams("App");
            bean.setCallType(0);//0普通用户 1专属用户
            bean.setQuestionGuideId(1);
            bean.setAppId("imyy.ymatou.com");
	        SendMessageCall call= service.setSendMessageCall(bean);
	        
	        ymt_queuesession queusesssion=queuesessionWapper.selectBy(MapUtil.hashMap("userId",userId)).get(0);
			//客服上线
			//service.getOnlineCS(csId);
	        
			activeaccessBean.setCsId(csId);
			activeaccessBean.setQueueSessionId(queusesssion.getId());
			activeaccessCall.setData(activeaccessBean);
			activeaccessCall.callService();			
			
			Logger.verifyEquals(2, activeaccessCall.getResult().getInt("ErrorCode"), "验证errorCode");
			Logger.verifyEquals(true, activeaccessCall.getResult().getString("ErrorMessage").contains("客服不存在"), "验证errorMessage");
			Logger.verifyEquals(false, activeaccessCall.getResult().getBoolean("Success"), "验证Success");
			Logger.verifyEquals("null", activeaccessCall.getResult().getString("SessionId"), "验证SessionId");
			Logger.verifyEquals("500", activeaccessCall.getStatus(), "验证Status");

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ActiveAccess_003() {
		Logger.start(true,"客服主动找会话队列发起会话-会话队列不存在");
		try {
			 //发去会话进入队列
			Logger.comment("用户发消息进入队列");
			SendMessageBean bean=service.setSendMessageBean();
		    bean.setUserId(userId);
            bean.setMessage("自动化测试_"+TestDataManager.getRandomIntNum(5));
            bean.setMessageSource(1);
            bean.setSourceParams("App");
            bean.setCallType(0);//0普通用户 1专属用户
            bean.setQuestionGuideId(1);
            bean.setAppId("imyy.ymatou.com");
	        SendMessageCall call= service.setSendMessageCall(bean);
	        
	        ymt_queuesession queusesssion=queuesessionWapper.selectBy(MapUtil.hashMap("userId",userId)).get(0);
			//客服上线
			service.getOnlineCS(csId);
	        
			activeaccessBean.setCsId(csId);
			activeaccessBean.setQueueSessionId(queusesssion.getId()+1);
			activeaccessCall.setData(activeaccessBean);
			activeaccessCall.callService();			
			
			Logger.verifyEquals(2, activeaccessCall.getResult().getInt("ErrorCode"), "验证errorCode");
			Logger.verifyEquals(true, activeaccessCall.getResult().getString("ErrorMessage").contains("会话队列不存在"), "验证errorMessage");
			Logger.verifyEquals(false, activeaccessCall.getResult().getBoolean("Success"), "验证Success");
			Logger.verifyEquals("null", activeaccessCall.getResult().getString("SessionId"), "验证SessionId");
			Logger.verifyEquals("500", activeaccessCall.getStatus(), "验证Status");

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ActiveAccess_004() {
		//Logger.start(true,"客服主动找会话队列发起会话-会话队列正被其他客服发起会话");
		Logger.start(true,"客服主动找会话队列发起会话-验证队列被锁定，客服主动发起会话失败");
		try {
            //发去会话进入队列
			Logger.comment("用户发消息进入队列");
			SendMessageBean bean=service.setSendMessageBean();
		    bean.setUserId(userId);
            bean.setMessage("自动化测试_"+TestDataManager.getRandomIntNum(5));
            bean.setMessageSource(1);
            bean.setSourceParams("App");
            bean.setCallType(0);//0普通用户 1专属用户
            bean.setQuestionGuideId(1);
            bean.setAppId("imyy.ymatou.com");
	        SendMessageCall call= service.setSendMessageCall(bean);
	        
	        ymt_queuesession queusesssion=queuesessionWapper.selectBy(MapUtil.hashMap("userId",userId)).get(0);
	        queusesssion.setIsLock(1);//修改锁定
	        queuesessionWapper.updateByPrimaryKey(queusesssion);
			//客服上线
			service.getOnlineCS(csId);
	        
			activeaccessBean.setCsId(csId);
			activeaccessBean.setQueueSessionId(queusesssion.getId());
			activeaccessCall.setData(activeaccessBean);
			activeaccessCall.callService();	
			
			Logger.verifyEquals(2, activeaccessCall.getResult().getInt("ErrorCode"), "验证errorCode");
			Logger.verifyEquals(true, activeaccessCall.getResult().getString("ErrorMessage").contains("客服主动发起会话失败"), "验证errorMessage");
			Logger.verifyEquals(false, activeaccessCall.getResult().getBoolean("Success"), "验证Success");
			Logger.verifyEquals("null", activeaccessCall.getResult().getString("SessionId"), "验证SessionId");
			Logger.verifyEquals("500", activeaccessCall.getStatus(), "验证Status");

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}